﻿/* 2013/6/27 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cosmos.Media
{
    class ID3V2FrameHeader
    {
        public const int TotalLength = 10;
        public const int FrameIDLength = 4;
        public const int SizeLength = 4;
        public const int FlagsLength = 2;

        public byte[] FrameID
        {
            get;
            private set;
        }

        public byte[] Size
        {
            get;
            private set;
        }

        public byte[] Flags
        {
            get;
            private set;
        }

        public long FrameSize
        {
            get
            {
                long frameSize
                    = Size[0] * 0x100000000L
                    + Size[1] * 0x10000L
                    + Size[2] * 0x100L
                    + Size[3];
                return frameSize;
            }
        }

        public ID3V2FrameHeader()
        {
            FrameID = new byte[FrameIDLength];
            Size = new byte[SizeLength];
            Flags = new byte[FlagsLength];
        }

        public void Load(byte[] bytes)
        {
            int i;
            int j;
            int position = 0;
            int currentIndex = 0;

            // Extracts frame ID.
            for (i = currentIndex, j = 0; i < currentIndex + ID3V2FrameHeader.FrameIDLength;
                i++, j++)
            {
                FrameID[j] = bytes[i];
                position++;
            }
            currentIndex = position;

            // Extracts size.
            for (i = currentIndex, j = 0; i < currentIndex + ID3V2FrameHeader.SizeLength;
                i++, j++)
            {
                Size[j] = bytes[i];
                position++;
            }
            currentIndex = position;

            // Extracts flags.
            for (i = currentIndex, j = 0; i < currentIndex + ID3V2FrameHeader.FlagsLength;
                i++, j++)
            {
                Flags[j] = bytes[i];
                position++;
            }
            currentIndex = position;
        }
    }
}
